#!/usr/bin/python
# -*- #coding:utf8

__author__ = '$Author: bo.feng $'
__version__ = '$Revision: 1.32 $'
__date__ = '$Date: 2011/03/25 10:54:03 $'


import MySQLdb
import datetime

def get_rs(conn, sql):
    """
    get record set by sql statement(select).
    """
    try:
        cursor = conn.cursor()
        cursor.execute(sql)
        rows = cursor.fetchall()
        conn.commit()
        return rows
    except:
        pass
    finally:
        try:
            cursor.close()
        except:
            pass

def exec_sql(conn, sql):
    """
    execute sql statement(insert,update,delete)
    """
    try:
        print sql
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
    except:
        pass;
    finally:
        try:
            cursor.close()
        except:
            pass

def get_last_workday_in_weekly(dt):
    dt_tmp = dt
    rows = get_rs(conn,"select * from WORKDAY where at_date = '%s'" % datetime.datetime.strftime(dt_tmp,'%Y-%m-%d'))
    if len(rows)>0:
        while(len(rows)>0 or (dt_tmp.weekday()>=0 and dt_tmp.weekday()<5)):
            dt_tmp = dt_tmp + datetime.timedelta(1)
            rows = get_rs(conn,"select * from WORKDAY where at_date = '%s'" % datetime.datetime.strftime(dt_tmp,'%Y-%m-%d'))
        return dt_tmp - datetime.timedelta(1)
    else:
        if dt_tmp.weekday()>=0 and dt_tmp.weekday()<5:
            while(dt_tmp.weekday()<5):
                dt_tmp = dt_tmp + datetime.timedelta(1)
            dt_tmp = dt_tmp - datetime.timedelta(1)
        else:
            while (dt_tmp.weekday()>=5):
                dt_tmp = dt_tmp - datetime.timedelta(1)
        return dt_tmp


def get_last_workday_in_monthly(dt):
    dt_tmp = datetime.date(dt.year, dt.month + 1, 1) - datetime.timedelta(1)
    rows = get_rs(conn,"select * from WORKDAY where '%s' = at_date" % datetime.datetime.strftime(dt_tmp,'%Y-%m-%d'))
    if(len(rows) > 0):
        return dt_tmp

    if dt_tmp.weekday()==6 :
        dt_tmp = dt_tmp - datetime.timedelta(1)
    if dt_tmp.weekday()==5:
        rows = get_rs(conn,"select * from WORKDAY where '%s' = at_date" % datetime.datetime.strftime(dt_tmp,'%Y-%m-%d'))
        if len(rows)>0 :
            return dt_tmp

    rows = get_rs(conn,"select * from HOLIDAY where '%s' between start_date and end_date" % datetime.datetime.strftime(dt_tmp,'%Y-%m-%d'))
    while(len(rows)>0):
        dt_tmp = dt_tmp - datetime.timedelta(1)
        rows = get_rs(conn,"select * from HOLIDAY where '%s' between start_date and end_date" % datetime.datetime.strftime(dt_tmp,'%Y-%m-%d'))
    return dt_tmp


tomorrow = datetime.date.today() + datetime.timedelta(1)
dt1 = datetime.datetime.strftime(tomorrow,'%Y-%m-%d')
conn = MySQLdb.connect(host='localhost',user='root',passwd='shzygjrmdwg', db='cprt')

rows = get_rs(conn,"select * from WORKDAY where '%s' = at_date" % dt1)
is_workday = rows != None and len(rows) > 0
if not is_workday :
    rows = get_rs(conn,"select * from HOLIDAY where '%s' between start_date and end_date" % dt1)
    is_holiday = rows != None and len(rows) > 0
if is_workday or ((not is_holiday) and tomorrow.weekday()>=0 and tomorrow.weekday()<5):
    dt2 = datetime.datetime.strftime(tomorrow + datetime.timedelta(1),'%Y-%m-%d')
    daily_schedule_user_rows= get_rs(conn=conn,sql='select user_id from USER_DETAIL where SCHEDULE_LIST & 1 = 1')
    for row in daily_schedule_user_rows:
        exec_sql(conn,"call check_schedule_for_user(%d,'%s','%s',1)" % (row[0],dt1,dt2 ))

    dt2 = get_last_workday_in_weekly(tomorrow)
    weekly_schedule_user_rows = get_rs(conn,'select user_id from USER_DETAIL where SCHEDULE_LIST & 2 = 2')
    for row in weekly_schedule_user_rows:
        exec_sql(conn,"call check_schedule_for_user(%d, '%s', '%s', 2) " %(row[0],dt1,dt2))

    dt2 = get_last_workday_in_monthly(tomorrow)
    monthly_schedule_user_rows = get_rs(conn,'select user_id from USER_DETAIL where SCHEDULE_LIST & 4 = 4')
    for row in monthly_schedule_user_rows:
        exec_sql(conn,"call check_schedule_for_user(%s,'%s', '%s', 4) " %(row[0], dt1,dt2))

conn.close()





